package com.jeff.tianti.cms.dao;

import com.jeff.tianti.cms.dto.AdvSpaceQueryDTO;
import com.jeff.tianti.cms.dto.CertificateQueryDTO;
import com.jeff.tianti.cms.entity.AdvSpace;
import com.jeff.tianti.cms.entity.Certificate;
import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xujianfang
 * @desc ArticleDaoImpl类
 * @date 2017-03-16
 */

public class CertificateDaoImpl extends CustomBaseSqlDaoImpl implements CertificateCustom {

    public PageModel<Certificate> queryCertificatePage(CertificateQueryDTO certificateQueryDTO) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select a from Certificate a where 1=1 ");
        if (certificateQueryDTO != null) {
            if (StringUtils.isNotBlank(certificateQueryDTO.getNumber())) {
                hql.append(" and a.number like :number ");
                map.put("number", "%"+certificateQueryDTO.getNumber()+"%");
            }

            if (StringUtils.isNotBlank(certificateQueryDTO.getNumber())&&!certificateQueryDTO.isNumberLike()) {
                hql.append(" and a.number = :number ");
                map.put("number", certificateQueryDTO.getNumber());
            }

            if (StringUtils.isNotBlank(certificateQueryDTO.getName())) {
                hql.append(" and a.name = :name ");
                map.put("name", certificateQueryDTO.getName());
            }

            if (StringUtils.isNotBlank(certificateQueryDTO.getColumnId())) {
                hql.append(" and a.columnId = :columnId ");
                map.put("columnId", certificateQueryDTO.getColumnId());
            }
        }
        return this.queryForPageWithParams(hql.toString(), map, certificateQueryDTO.getCurrentPage(), certificateQueryDTO.getPageSize());
    }

    @Override
    public int updateSelectiveById(Certificate certificate) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("update Certificate t set");
        if (certificate != null) {
            if (!StringUtils.isNotBlank(certificate.getId())) {
                throw new RuntimeException("id 不能为空");
            }

            if (StringUtils.isNotBlank(certificate.getNumber())) {
                hql.append(" t.number = :number ");
                map.put("number", certificate.getNumber());
            }

            if (StringUtils.isNotBlank(certificate.getImg())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.img = :img ");
                } else {
                    hql.append(" t.img = :img ");
                }
                map.put("img", certificate.getImg());
            }
            if (StringUtils.isNotBlank(certificate.getColumnId())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.columnId = :columnId ");
                } else {
                    hql.append(" t.columnId = :columnId ");
                }
                map.put("columnId", certificate.getColumnId());
            }

            if (StringUtils.isNotBlank(certificate.getName())) {

                if (hql.toString().contains("=")) {
                    hql.append(" ,t.name = :name ");
                } else {
                    hql.append(" t.name = :name ");
                }
                map.put("name", certificate.getName());
            }


            hql.append("where t.id=:id ");
            map.put("id", certificate.getId());
        }
        return this.update(hql.toString(), map);
    }
}
